package com.letwal.cmcvnd.me

import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import android.view.View
import android.webkit.WebSettings
import androidx.lifecycle.ViewModel
import com.letwal.cmcvnd.base.BaseActivity
import com.letwal.cmcvnd.databinding.ActivityTermsOfUseWebViewBinding
import com.letwal.cmcvnd.enums.PageTagEnum
import com.letwal.cmcvnd.extend.*
import com.letwal.cmcvnd.webview.AppJavaScriptImplement
import com.letwal.cmcvnd.webview.AppJavaScriptInterface


/** webview页 */
@SuppressLint("CustomSplashScreen")
class TermsOfUseWebViewActivity : BaseActivity(), AppJavaScriptImplement {

    //布局binding
    private lateinit var mActivityBinding: ActivityTermsOfUseWebViewBinding

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.app_web_view.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityTermsOfUseWebViewBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun onContentViewBinded() {
        try {
            //清理缓存
            mActivityBinding.webAppWebView.clearCache(true);
            mActivityBinding.webAppWebView.clearHistory();
            mActivityBinding.webAppWebView.clearFormData();

            val webSettings: WebSettings = mActivityBinding.webAppWebView.settings
            try {
                webSettings.setSupportZoom(true)
                webSettings.javaScriptEnabled = true
                webSettings.domStorageEnabled = true
                webSettings.allowContentAccess = true
                webSettings.useWideViewPort = true
                webSettings.loadWithOverviewMode = true
                webSettings.builtInZoomControls = true
                webSettings.displayZoomControls = false
                webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
                webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL
                webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            } catch (e: Exception) {
                e.printStackTrace()
            }

            // 创建一个 Java 对象，它将注入到 H5 页面中
            val appJavaScriptInterface = AppJavaScriptInterface(this)
            mActivityBinding.webAppWebView.addJavascriptInterface(appJavaScriptInterface, "android")

            //加载网页
            val url =
//                "http://110.238.78.183/launch?androidId=f07aa6e5a29892b2486c779f8db69254&advertId=f07aa6e5a29892b2486c779f8db69254&token=2f0ba3f59a4b772e47065fbe1596ea33"
//                "http://110.238.78.183?androidId=f07aa6e5a29892b2486c779f8db69254&advertId=f07aa6e5a29892b2486c779f8db69254&token=2f0ba3f59a4b772e47065fbe1596ea33"
                "https://www.cmcwallet.net/termsofuse"

            Handler(Looper.getMainLooper()).postDelayed({
                mActivityBinding.webAppWebView.loadUrl(url)
            }, 1000)

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun queryUserState(msg:String) {
        println("======== queryUserState $msg")
    }

    override fun openSysBrowser(msg:String) {
        println("======== openSysBrowser $msg")
    }

    override fun toHomeActivity(msg:String) {
        println("======== toHomeActivity $msg")
    }
}